package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.service.PerformanceSetService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.performanceset.pojo.dto.PerformanceSetCheckDTO;
import com.xbongbong.pro.performanceset.pojo.dto.PerformanceSetDeleteDTO;
import com.xbongbong.pro.performanceset.pojo.dto.PerformanceSetDetailDTO;
import com.xbongbong.pro.performanceset.pojo.dto.PerformanceSetEditDTO;
import com.xbongbong.pro.performanceset.pojo.dto.PerformanceSetEnableDTO;
import com.xbongbong.pro.performanceset.pojo.dto.PerformanceSetInitDTO;
import com.xbongbong.pro.performanceset.pojo.dto.PerformanceSetListByPermissionDTO;
import com.xbongbong.pro.performanceset.pojo.dto.PerformanceSetListDTO;
import com.xbongbong.pro.performanceset.pojo.dto.PerformanceSetPermissionListDTO;
import com.xbongbong.pro.performanceset.pojo.dto.PerformanceSetProductListDTO;
import com.xbongbong.pro.performanceset.pojo.dto.PerformanceSetSaveDTO;
import com.xbongbong.pro.performanceset.pojo.dto.PerformanceSetSaveGuideDTO;
import com.xbongbong.pro.performanceset.pojo.vo.PerformanceSetCheckVO;
import com.xbongbong.pro.performanceset.pojo.vo.PerformanceSetDeleteVO;
import com.xbongbong.pro.performanceset.pojo.vo.PerformanceSetDetailVO;
import com.xbongbong.pro.performanceset.pojo.vo.PerformanceSetEditVO;
import com.xbongbong.pro.performanceset.pojo.vo.PerformanceSetEnableVO;
import com.xbongbong.pro.performanceset.pojo.vo.PerformanceSetInitVO;
import com.xbongbong.pro.performanceset.pojo.vo.PerformanceSetListByPermissionVO;
import com.xbongbong.pro.performanceset.pojo.vo.PerformanceSetListVO;
import com.xbongbong.pro.performanceset.pojo.vo.PerformanceSetPermissionListVO;
import com.xbongbong.pro.performanceset.pojo.vo.PerformanceSetSaveGuideVO;
import com.xbongbong.pro.performanceset.pojo.vo.PerformanceSetSaveVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @description: 业绩目标指标
 * @Author: hongxiao
 * @date: 2019-01-25 10:41
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/performanceSet")
public class PerformanceSetController {

    @Resource
    private PerformanceSetService performanceSetService;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    /**
     * 引导页第一步
     * @param performanceSetInitDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-01-30 15:27
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/init", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String init(@RequestBody @Valid PerformanceSetInitDTO performanceSetInitDTO, BindingResult br) {
        XbbResponse<PerformanceSetInitVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(performanceSetInitDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(performanceSetInitDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                PerformanceSetInitVO performanceSetInitVO = performanceSetService.init(performanceSetInitDTO);
                response = new XbbResponse<>(performanceSetInitVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 引导页（一次性保存多项设置）
     * @param performanceSetSaveGuideDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-01-30 15:31
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/saveGuide", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String saveGuide(@RequestBody @Valid PerformanceSetSaveGuideDTO performanceSetSaveGuideDTO, BindingResult br) {
        XbbResponse<PerformanceSetSaveGuideVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(performanceSetSaveGuideDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(performanceSetSaveGuideDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                PerformanceSetSaveGuideVO performanceSetSaveGuideVO = performanceSetService.saveGuide(performanceSetSaveGuideDTO);
                response = new XbbResponse<>(performanceSetSaveGuideVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 业绩目标指标列表
     * @param performanceSetListDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-01-31 13:50
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid PerformanceSetListDTO performanceSetListDTO, BindingResult br) {
        XbbResponse<PerformanceSetListVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PerformanceSetListVO performanceSetListVO = performanceSetService.list(performanceSetListDTO);
                response = new XbbResponse<>(performanceSetListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 业绩目标指标详情
     * @param performanceSetDetailDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-01-31 14:14
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String detail(@RequestBody @Valid PerformanceSetDetailDTO performanceSetDetailDTO, BindingResult br) {
        XbbResponse<PerformanceSetDetailVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PerformanceSetDetailVO performanceSetDetailVO = performanceSetService.detail(performanceSetDetailDTO);
                response = new XbbResponse<>(performanceSetDetailVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 编辑业绩目标指标
     * @param performanceSetEditlDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-01-31 19:12
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String edit(@RequestBody @Valid PerformanceSetEditDTO performanceSetEditlDTO, BindingResult br) {
        XbbResponse<PerformanceSetEditVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PerformanceSetEditVO performanceSetEditVO = performanceSetService.edit(performanceSetEditlDTO);
                response = new XbbResponse<>(performanceSetEditVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 保存业绩目标指标
     * @param performanceSetSavelDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-01 09:12
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String save(@RequestBody @Valid PerformanceSetSaveDTO performanceSetSavelDTO, BindingResult br) {
        XbbResponse<PerformanceSetSaveVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PerformanceSetSaveVO performanceSetSaveVO = performanceSetService.save(performanceSetSavelDTO);
                response = new XbbResponse<>(performanceSetSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 删除业绩目标指标
     * @param performanceSetDeleteDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-01 14:49
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid PerformanceSetDeleteDTO performanceSetDeleteDTO, BindingResult br) {
        XbbResponse<PerformanceSetDeleteVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PerformanceSetDeleteVO performanceSetDeleteVO = performanceSetService.delete(performanceSetDeleteDTO);
                response = new XbbResponse<>(performanceSetDeleteVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 启用业绩目标指标
     * @param performanceSetEnableDTO 入参
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-01 14:50
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/enable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String enable(@RequestBody @Valid PerformanceSetEnableDTO performanceSetEnableDTO, BindingResult br) {
        XbbResponse<PerformanceSetEnableVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PerformanceSetEnableVO performanceSetEnableVO = performanceSetService.enable(performanceSetEnableDTO);
                response = new XbbResponse<>(performanceSetEnableVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 判断是否配置过指标
     * @param performanceSetCheckDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-15 09:32
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/check", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String check(@RequestBody @Valid PerformanceSetCheckDTO performanceSetCheckDTO, BindingResult br) {
        XbbResponse<PerformanceSetCheckVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PerformanceSetCheckVO performanceSetCheckVO = performanceSetService.check(performanceSetCheckDTO);
                response = new XbbResponse<>(performanceSetCheckVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取登录员工能看到的指标列表，参数为空
     * @param performanceSetListByPermissionDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-15 13:59
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/listByPermission", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String listByPermission(@RequestBody @Valid PerformanceSetListByPermissionDTO performanceSetListByPermissionDTO, BindingResult br) {
        XbbResponse<PerformanceSetListByPermissionVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PerformanceSetListByPermissionVO performanceSetListByPermissionVO = performanceSetService.listByPermission(performanceSetListByPermissionDTO);
                response = new XbbResponse<>(performanceSetListByPermissionVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取考核对象[员工/部门]列表
     * @param performanceSetPermissionListDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-15 21:06
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/permissionList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String permissionList(@RequestBody @Valid PerformanceSetPermissionListDTO performanceSetPermissionListDTO, BindingResult br) {
        XbbResponse<PerformanceSetPermissionListVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PerformanceSetPermissionListVO performanceSetPermissionListVO = performanceSetService.permissionList(performanceSetPermissionListDTO);
                response = new XbbResponse<>(performanceSetPermissionListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取考核对象产品列表
     * @param performanceSetProductListDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author haibin.zhang
     * @date: 2019-05-22 9:46
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/productList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String productList(@RequestBody @Valid PerformanceSetProductListDTO performanceSetProductListDTO, BindingResult br) {
        XbbResponse<PerformanceSetPermissionListVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PerformanceSetPermissionListVO performanceSetPermissionListVO = performanceSetService.productList(performanceSetProductListDTO);
                response = new XbbResponse<>(performanceSetPermissionListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

}
